/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#include "launcheritemhelper.h"
#include "launcheritem.h"
#include "modeldefs.h"
#include "singleton.h"
#include "core/appmanager.h"
#include "types.h"

LauncherItem *LauncherItemHelper::createAppIconLauncherItem(const UKUI::IconItem *iconItem, int page)
{
    LauncherItem *appIconItem = createBaseLauncherItem(iconItem, page);
    appIconItem->setIcon(iconItem->getIconName());
    appIconItem->setDesktopFile(iconItem->getDesktopName());
    appIconItem->setType(LauncherItem::AppIcon);
    appIconItem->setUninstallable(!existsInWhiteList(appIconItem->desktopFile()));
    return appIconItem;
}

std::tuple<LauncherItem *, model::MultiPageModel *> LauncherItemHelper::
                createAppGroupLauncherItem(const UKUI::GroupItem *groupItem, 
                                           const QList<QList<UKUI::IconItem *>> &childrenItem, 
                                           int page)
{
    LauncherItem *appGroupItem = createBaseLauncherItem(groupItem, page);
    appGroupItem->setUninstallable(false);
    model::MultiPageModel *groupModel = new model::MultiPageModel;
    int pageIndex = 0;
    for (auto &childrenItemEachPage : qAsConst(childrenItem)) {
        model::PageModel *pageModel = new model::PageModel;
        for (auto &childItem : qAsConst(childrenItemEachPage)) {
            LauncherItem *_childItem = createAppIconLauncherItem(childItem, pageIndex);
            pageModel->append(_childItem);
        }
        groupModel->append(pageModel);
        ++ pageIndex;
    }
    appGroupItem->setModel(groupModel);
    return std::make_tuple(appGroupItem, groupModel);
}

std::tuple<LauncherItem *, model::MultiPageModel *> LauncherItemHelper::
                createAppContainerLauncherItem(const UKUI::ContainerItem *containerItem, 
                                               const QList<QList<UKUI::IconItem *>> &childrenItem, 
                                               int page)
{
    auto *groupItem = containerItem2GroupItem(containerItem);
    LauncherItem *launcherItem;
    model::MultiPageModel *containerModel;
    std::tie (launcherItem, containerModel) = createAppGroupLauncherItem(groupItem, childrenItem, page);
    launcherItem->setType(LauncherItem::Container);
    launcherItem->setTitleEditable(containerItem->isTitleEditable());
    launcherItem->setContainerEditable(containerItem->isContainerEditable());
    // add launcheritem

    return std::make_tuple(launcherItem, containerModel);
}

LauncherItem *LauncherItemHelper::createAddIconItem(int page, int state)
{
    LauncherItem *addItem = new LauncherItem;
    addItem->setId(0);
    addItem->setPageIndex(page);
    addItem->setType(LauncherItem::AddIcon);
    addItem->setIcon("qrc:/ui/images/ukui_add_default.svg");
    addItem->setName(QObject::tr("Add"));
    addItem->setRowSpan(1);
    addItem->setColumnSpan(1);
    addItem->setState(state);
    return addItem;
}

LauncherItem *LauncherItemHelper::createWidgetItem(const UKUI::Widget *widgetItem, int page)
{
    LauncherItem *_widgetItem = createBaseLauncherItem(widgetItem, page);
    _widgetItem->setType(LauncherItem::Widget);
    _widgetItem->setQmlFile(widgetItem->getQmlFilePath());
    return _widgetItem;
}

LauncherItem *LauncherItemHelper::createBaseLauncherItem(const UKUI::BaseItem *baseItem, int page)
{
    LauncherItem *launcherItem = new LauncherItem;
    launcherItem->setId(baseItem->getId());
    launcherItem->setName(baseItem->getName());
    launcherItem->setCategories(baseItem->getCategories());
    launcherItem->setRowSpan(baseItem->getPlaceHolder().constFirst());
    launcherItem->setColumnSpan(baseItem->getPlaceHolder().constLast());
    launcherItem->setPageIndex(page);
    return launcherItem;
}

UKUI::GroupItem *LauncherItemHelper::containerItem2GroupItem(const UKUI::ContainerItem *containerItem)
{
    UKUI::GroupItem *groupItem = new UKUI::GroupItem(containerItem->getName(), containerItem->getId());
    return groupItem;
}

bool LauncherItemHelper::existsInWhiteList(const QString &desktopFileName)
{
    auto appWhiteList = common::Singleton<AppManager>::GetInstance().getAppWhiteList();
    for (auto &app : qAsConst(appWhiteList)) {
        if (desktopFileName.endsWith(app)) {
            return true;
        }
    }

    return false;
}
